package com.wy2.eclipse.tool.lib.common;

import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.PlatformUI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ibm.icu.text.SimpleDateFormat;

/***
 * Ide工具类
 * @author nnsword
 *
 */
public class IDEUtils {
	private static final Logger log = LoggerFactory.getLogger(IDEUtils.class);
	
	
	/**
	 * 取得Eclipse安装目录
	 * @return
	 */
	public static IPath getInstallLocation() {
		Location loc=Platform.getInstallLocation();
		if(loc!=null) {
			URL url = loc.getURL();
			IPath path = Path.fromOSString(url.getFile());
			return path;
		}
		return null;
	}
	/**
	 * 配置目录
	 * @return
	 */
	public static String getConfigPath() {
		//String path= IDEUtils.getInstallLocation().toOSString()+"jdpdata";
		File file=new File(IDEUtils.getInstallLocation().toOSString(), Constants.CONFIG_DIRECTORY);
		if(!file.exists()) {
			file.mkdir();
		}
		return file.getAbsolutePath();
	}
	/**
	 * 首选项存储路径
	 * @param fileName
	 * @return
	 */
	public static String getPreferenceStorePath(String fileName) {
		File storeDirectory=new File(getConfigPath(), Constants.CONFIG_PREFERENCE_STORE_DIRECTORY);
		if(!storeDirectory.exists()) {
			storeDirectory.mkdirs();
		}
		File file=new File(storeDirectory, fileName);
		return file.getAbsolutePath();
	}
	
	public  static void refreshProject(IProject project) {
            try {
                project.refreshLocal(IResource.DEPTH_INFINITE, null);
                log.debug(project.getName()+"=>项目刷新完成");
            } catch (CoreException e) {
                log.error(project.getName()+"=>刷新项目",e);
            }
    }
	public  static void refreshProjects(List<IProject> projects) {
		for (IProject iProject : projects) {
			try {
				iProject.refreshLocal(IResource.DEPTH_INFINITE, null);
				log.debug(iProject.getName()+"=>项目刷新完成");
			} catch (CoreException e) {
				log.error(iProject.getName()+"=>刷新项目",e);
			}
		}
	}
	public static void refreshFile(String filePath) {
		
		try {
			IFile f = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(filePath));
			f.refreshLocal(IResource.DEPTH_ZERO, null);
			log.debug(filePath+"=>刷新文件完成");
		} catch (CoreException e) {
			log.error(filePath+"=>刷新文件失败",e);
		}
	}

	/**
	 * 获取选中工程
	 * @return
	 */
	public static IProject getSelectProject(){  
		ISelectionService selectionService =PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService(); 
  
        ISelection selection = selectionService.getSelection();  
  
        IProject project = null;  
        if(selection instanceof IStructuredSelection) {  
            Object element = ((IStructuredSelection)selection).getFirstElement();  
  
            if (element instanceof IResource) {  
                project= ((IResource)element).getProject();  
            } else if (element instanceof IJavaElement) {  
                IJavaProject jProject= ((IJavaElement)element).getJavaProject();  
                project = jProject.getProject();  
            }  
        }   
        return project;  
    }  
	/**
	 * 获取工作空间
	 * @return
	 */
	public static IWorkspaceRoot getWorkspaceRoot() {
		return ResourcesPlugin.getWorkspace().getRoot();
	}
	/**
	 * 获取工作空间位置
	 * @return
	 */
	public static IPath getWorkspaceLocation() {
		return getWorkspaceRoot().getLocation();
	}
	/**
	 * 取工作空间下的所有项目
	 * @return
	 */
	public static IProject[] getWorkspaceProjects(){
		return getWorkspaceRoot().getProjects();
	}
	/**
	 * 取工作空间下项目
	 * @param projectName
	 * @return
	 */
	public static IProject getWorkspaceProject(String projectName){
		return getWorkspaceRoot().getProject(projectName);
	}
}
